colorAdminApp.controller('roleController', function($scope,$compile,$rootScope,$http,$state,$stateParams,modalService) {
	
	$scope.easyNgPagination={
			options:{
				        currentPage:1,
				        totalCount:0,
				        pageSize:10,
			            onPageChange: function(){
			 		            	$scope.loadData();
			            }
			}
	};
	$scope.q={"nameZh":"","nameEn":""};
	$scope.items=[];
	$scope.loadData=function(){
		var currentPage=$scope.easyNgPagination.options.currentPage-1;
		var entity={"nameZh":$scope.q.nameZh,"nameZh":$scope.q.nameEn,"page":currentPage,"size":$scope.easyNgPagination.options.pageSize};
		$(".dataTables_wrapper").block({ message:'正在加载数据，请稍等...',css: { top: '50%' } });
		$http.post('api/core/access/role/findByPage',{params:entity}).success(function(data, status, headers, config) {
			$scope.items=data.data.content;
			$scope.easyNgPagination.options.totalCount=data.data.totalElements;
			$(".dataTables_wrapper").unblock();
		}).error(function(data, status, headers, config) {
	    	alert(data);
        });  
	};
	
	$scope.showEditModal=function(entity){
		$scope.editEntity=entity;
		$("#modal-edit").modal("show");	
	};
	
	$scope.showAddModal=function(){
		$("#modal-add").modal("show");	
	};
	
	$scope.update=function(entity){
		delete entity.createTime;
	  	var requestJson=JSON.stringify(entity);
		$(".modal-dialog").block({message:'正在执行操作，请稍等...'});
		$http.post('api/core/access/role/update',{params:{"requestJson":requestJson}}).success(function(data, status, headers, config) {
			$(".modal-dialog").unblock();
			modalService.showAlertModal('注意',data.message);
			if(data.status==1){
				$("#modal-edit").modal("hide");
				$scope.loadData();
			}
		}).error(function(data, status, headers, config) {
			$(".modal-dialog").unblock();
        });  
	};
	
    $scope.save=function(entity){
    	var requestJson=JSON.stringify(entity);
   		$(".modal-dialog").block({message:'正在执行操作，请稍等...'});
   		$http.post('api/core/access/role/add',{params:{"requestJson":requestJson}}).success(function(data, status, headers, config) {
			$(".modal-dialog").unblock();
			modalService.showAlertModal('注意',data.message);
			if(data.status==1){
				$("#modal-add").modal("hide");
				$scope.loadData();
			}
		}).error(function(data, status, headers, config) {
			$(".modal-dialog").unblock();
        });  
	};
	
	$scope.doDelete=function(item){
		if(!confirm("确认删除 “" + item.nameZh + "” 吗？")){
			return ;
		}
		$("body").block({ message:'正在执行操作，请稍等...',css: { top: '50%' } });
		$http.post('api/core/access/role/deleteById',{params:{"id":item.id}}).success(function(data, status, headers, config) {
			modalService.showAlertModal('注意',data.message);
			$("body").unblock();
			if(data.status==1){
				$scope.loadData(); 
			}
		}).error(function(data, status, headers, config) {
			$("body").unblock();
	    
       });  
	};
    
    $scope.showPermissionTreeModal=function(item){
    	 $scope.editPermissionEntity=item;
    	 $scope.loadMenuTree(item.id,'MENU');
    	 $scope.loadMenuTree(item.id,'FUNCTION_POINT');
    	$("#permissionTreeModal").modal("show");
    };
    
	$scope.activeTab=0;
    $scope.changeTab=function(index){//切换权限设置tab
    	$scope.activeTab=index;
    };
  
    $scope.loadMenuTree=function(roleId,resourcesType){
    	var submitData={};
    	submitData["roleId"]=roleId;
    	submitData["resourcesType"]=resourcesType;
   		$http.post('api/core/access/resources/getAllWithCheckBoxZtreeJson',{params:submitData}).success(function(data, status, headers, config) {
			if(resourcesType=="MENU"){
				 $scope.initTree(data.data,"menuTree");
			}else{
				 $scope.initTree(data.data,"functionTree");
			}
		}).error(function(data, status, headers, config) {
	    	
        });  
    };
    
    
    $scope.initTree=function(zNodes,treeId){
    	var setting = {
    			check: {
    				enable: true
    			},
    			data: {
    				simpleData: {
    					enable: true
    				}
    			}
    		};
    	$.fn.zTree.init($("#"+treeId), setting, zNodes);	
    	$.fn.zTree.getZTreeObj(treeId).setting.check.chkboxType={ "Y" : "ps", "N" : "ps" };
    };
    
    $scope.saveRescRole=function(treeId,resourcesType){
    	var treeObj=$.fn.zTree.getZTreeObj(treeId);
    	var nodes=treeObj.getCheckedNodes(true);
    	var submitData={};
    	submitData["roleId"]=$scope.editPermissionEntity.id;
    	submitData["resourcesType"]=resourcesType;
       	submitData["resourcesIds"]=$scope.getNodesId(nodes).join(",");
    	$("#permissionTreeModal").block({ message:'正在执行操作，请稍等...' });
    	$http.post('api/core/access/resc-role/update',{params:submitData}).success(function(data, status, headers, config) {
    		 $("#permissionTreeModal").unblock();
			 $('#permissionTreeModal').modal('hide');
			 modalService.showAlertModal('注意',data.message);
		}).error(function(data, status, headers, config) {
			 $("#permissionTreeModal").unblock();
       });  
    };

    $scope.getNodesId=function(nodes){
    	var retVal=new Array();
    	for(var i=0;i<nodes.length;i++){
    		retVal.push(nodes[i].id);
    	}
    	return retVal;
    }
	
});
